package com.uetoken.cn;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Build;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.content.ContextCompat;
import android.view.KeyEvent;

import com.blankj.utilcode.util.BarUtils;
import com.blankj.utilcode.util.ToastUtils;
import com.qmuiteam.qmui.util.QMUIResHelper;
import com.qmuiteam.qmui.util.QMUIStatusBarHelper;
import com.qmuiteam.qmui.widget.QMUITabSegment;
import com.qmuiteam.qmui.widget.QMUIViewPager;
import com.uetoken.cn.base.BaseActivity;
import com.uetoken.cn.fragment.AssetsFragment;
import com.uetoken.cn.fragment.MeFragment;

import java.util.HashMap;

import butterknife.BindView;
import butterknife.ButterKnife;

public class MainActivity extends BaseActivity {

    @BindView(R.id.pager)
    QMUIViewPager mViewPager;
    @BindView(R.id.tabs)
    QMUITabSegment mTabSegment;
    private HashMap<Pager, Fragment> mPages;
    private long exitTime = 0;
    private PaySuccessBroadcastReceiver mReceiver;

    public static MainActivity mMainActivity;
    private FragmentPagerAdapter mFragmentPagerAdapter = new FragmentPagerAdapter(getSupportFragmentManager()) {
        @Override
        public int getCount() {
            return mPages.size();
        }

        @Override
        public Fragment getItem(int position) {
            return mPages.get(Pager.getPagerFromPositon(position));
        }
    };
    private AssetsFragment assetsFragment;
    private MeFragment meFragment;

    @Override
    public int getLayoutId() {
        return R.layout.activity_main;
    }

    @Override
    public void initView() {
        mMainActivity = this;
        ButterKnife.bind(this);
        // 沉浸式状态栏
        QMUIStatusBarHelper.translucent(this);
        initTabs();
        initPagers();
    }

    @Override
    public void initData() {
        mReceiver = new PaySuccessBroadcastReceiver();
        IntentFilter intentFilter = new IntentFilter();
        intentFilter.addAction("paySuccess");
        registerReceiver(mReceiver, intentFilter);

    }

    @Override
    public void init() {

    }

    private void initTabs() {
        int normalColor = QMUIResHelper.getAttrColor(this, R.attr.qmui_config_color_gray_6);
        int selectColor = QMUIResHelper.getAttrColor(this, R.attr.qmui_config_color_blue);
        mTabSegment.setDefaultNormalColor(normalColor);
        mTabSegment.setDefaultSelectedColor(selectColor);
        QMUITabSegment.Tab component = new QMUITabSegment.Tab(
                ContextCompat.getDrawable(this, R.mipmap.icon_tabbar_property),
                ContextCompat.getDrawable(this, R.mipmap.icon_tabbar_property_selected),
                getResources().getString(R.string.tabbar_property_txt), false
        );
        QMUITabSegment.Tab util = new QMUITabSegment.Tab(
                ContextCompat.getDrawable(this, R.mipmap.icon_tabbar_my),
                ContextCompat.getDrawable(this, R.mipmap.icon_tabbar_my_selected),
                getResources().getString(R.string.tabbar_my_txt), false
        );
        mTabSegment.addTab(component)
                .addTab(util);
    }

    private void initPagers() {
        mPages = new HashMap<>();
        assetsFragment = new AssetsFragment();
        mPages.put(Pager.PROPERTY, assetsFragment);

        meFragment = new MeFragment();
        mPages.put(Pager.MY, meFragment);

        mViewPager.setAdapter(mFragmentPagerAdapter);
        mTabSegment.setupWithViewPager(mViewPager, false);

    }

    enum Pager {
        PROPERTY, MY;

        public static Pager getPagerFromPositon(int position) {
            switch (position) {
                case 0:
                    return PROPERTY;
                case 1:
                    return MY;
                default:
                    return PROPERTY;
            }
        }
    }


    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            if ((System.currentTimeMillis() - exitTime) > 2000) {
                ToastUtils.showShort(getResources().getString(R.string.str_click_again));
                exitTime = System.currentTimeMillis();
            } else {
                finish();
                System.exit(0);
            }
            return true;
        }

        return super.onKeyDown(keyCode, event);

    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        unregisterReceiver(mReceiver);
    }

    class PaySuccessBroadcastReceiver extends BroadcastReceiver {
        @Override
        public void onReceive(Context context, Intent intent) {
            assetsFragment.initHeadViewAndData();
            meFragment.initHeadView();
        }
    }
}
